using AutoMapper;
using Tools.Domain.Common.Repositories.Contracts;
using Webshop20.Application.DataTransferObjects;
using Webshop20.Application.Queries.Messages;
using Webshop20.Domain.Common.Exceptions;
using Webshop20.Domain.Customers.Core;
using Webshop20.Domain.Customers.DomainMessages;

namespace Webshop20.Application.Queries.Services
{

    public interface IGetCustomerInformationService : IService{}

    public class GetCustomerInformationService : IGetCustomerInformationService
    {
        private IRepository<Customer> customerRepository;

        public GetCustomerInformationService(IRepository<Customer> customerRepository)
        {
            this.customerRepository = customerRepository;
        }

        public ISystemMessage Handle(ISystemMessage request)
        {

                var customerRequest = (GetCustomerInformationRequest)request;
                var customer = customerRepository.FindById(customerRequest.CustomerId);
                if(customer.IsInPendigState())
                {
                    return new GetCustomerInformationResponse(new AdressNotSet(), null);
                }
            
                var customerDTO = (CustomerDTO)Mapper.Map(customer, typeof(Customer), typeof(CustomerDTO));
                customerDTO = (CustomerDTO)Mapper.Map(customer.Adress, customerDTO, typeof(Adress), typeof(CustomerDTO));

                return new GetCustomerInformationResponse(null, customerDTO);

        }
    }
}